import { _decorator, Vec3, Node,tween,Tween,v3,TweenSystem } from 'cc';
import { Room } from './room';
import { Util } from './util';
const { ccclass, property } = _decorator;

@ccclass('Assassin')
export class Assassin  {
    static speed = 300

    node: Node
    //startPositon: Vec3
    cornerPosition: Vec3
    targetRoom:Room

    path: Array<Vec3>

    animation:Tween<Node>

    public constructor(node: Node, cornerPosition:Vec3) {
        this.node = node;
        this.cornerPosition = cornerPosition

        
    }

    setTargetRoom(targetRoom:Room) {
        this.targetRoom = targetRoom;
        this.path = [this.node.getPosition(), this.cornerPosition, ...this.targetRoom.path]
    }

    pause() {
        TweenSystem.instance.ActionManager.pauseTarget(this.node)
    }

    resume() {
        TweenSystem.instance.ActionManager.resumeTarget(this.node)
    }


    move(func: () => void) {
        this.animation = tween(this.node)

        let start = this.path[0]
        for (let p of this.path) {
            let duration = Util.getDistance(start, p) / Assassin.speed
            this.animation.to(duration, { position: p})
            start = p
        }

        this.animation.call(() => { 
            func()
        })
        .start()        
    }

    back(func: () => void) {
        let t = tween(this.node)

        let reversePath:Array<Vec3> = []
        for (let p of this.path) {
            reversePath.unshift(p)
        }
        
        let start = reversePath[0]
        for (let p of reversePath) {
            let duration = Util.getDistance(start, p) / Assassin.speed
            t.to(duration, { position: p})
            start = p
        }

         t.call(func)
        .start()        
    }

}

